package com.media.themovieworld.player;

import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.MediaPlayer.OnBufferingUpdateListener;
import io.vov.vitamio.MediaPlayer.OnCompletionListener;
import io.vov.vitamio.MediaPlayer.OnInfoListener;
import io.vov.vitamio.MediaPlayer.OnPreparedListener;
import io.vov.vitamio.utils.Log;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.Toast;

import com.media.themovieworld.R;
import com.media.themovieworld.model.FilmPartModel;
import com.media.themovieworld.util.AppSettingPrefference;

public class VideoViewController implements OnPreparedListener, OnCompletionListener, OnInfoListener, OnBufferingUpdateListener {

	public static final String VIDEO_QUALITY_240P = "240p";
	public static final String VIDEO_QUALITY_360p = "360p";
	public static final String VIDEO_QUALITY_720p = "720p";
	
	public static final String VIDEO_CHROMA_RGB565 = "RGB565";
	public static final String VIDEO_CHROMA_RGBA8888 = "RGBA8888";
	
	private static final String LOG_TAG = "VideoViewController";
	private Activity mActivity;
	private String path = "http://clips.vorwaerts-gmbh.de/VfE_html5.mp4";
	private String streamUrl = "rtsp://117.120.31.83:8010/stream/_definst_/movies/film/hoat_hinh/33531376.mp4";
	private ArrayList<FilmPartModel> mListFilmPart;
	private VideoView mVideoView;
	private ProgressBar mProgressBar;
	private Spinner qualitySpinner;
	private Spinner filmPartSpinner;
	
	private int partIndex;
	
	private long currentPosition = 0;
	
	public VideoViewController(Activity context){
		mActivity = context;
		mListFilmPart = FilmPartManage.getInstance().getCurrentListFilmPart();
	}
	
	public void setPartIndex(int index){
		partIndex = index;
	}
	
	public void setRootView(View root){
		mVideoView = (VideoView) root.findViewById(R.id.surface_view);
		mProgressBar = (ProgressBar)root.findViewById(R.id.tmw__videoview_progressBar);
		qualitySpinner = (Spinner) root.findViewById(R.id.tmw__video_player_activity_ics_quality_border);
		filmPartSpinner = (Spinner) root.findViewById(R.id.tmw__video_player_activity_ics_part_border);
		initQualitySpinner();
		initFilmPartSpinner();
		initVideoView();
	}
	
	private void initQualitySpinner() {
		String[] list = mActivity.getResources().getStringArray(R.array.quality);
		final ArrayList<String> listQuality = new ArrayList<String>(Arrays.asList(list));
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mActivity, android.R.layout.simple_spinner_item, listQuality);
		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		qualitySpinner.setAdapter(dataAdapter);
		String quality = AppSettingPrefference.getInstance().getMovieQuality();
		int currentQuality = getItemPosition(quality, listQuality);
		qualitySpinner.setSelection(currentQuality == -1 ? 1:currentQuality);
		qualitySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View row,
					int pos, long arg3) {
				Toast.makeText(mActivity, "select "+listQuality.get(pos)+" quality", Toast.LENGTH_SHORT).show();
				mVideoView.setVideoQuality(getVideoQuality(listQuality.get(pos)));
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}
	
	private void initFilmPartSpinner(){
		if (mListFilmPart==null) return;
		ArrayList<String> listFilm = new ArrayList<String>();
		for (int i = 0; i < mListFilmPart.size(); i++) {
			listFilm.add(mListFilmPart.get(i).name);
		}
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mActivity, android.R.layout.simple_spinner_item, listFilm);
		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		filmPartSpinner.setAdapter(dataAdapter);
		filmPartSpinner.setSelection(partIndex);
		filmPartSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View row,
					int pos, long arg3) {
				partIndex = pos;
				mVideoView.setVideoPath(changeLinkToStream(mListFilmPart.
						get(partIndex>(mListFilmPart.size()) ? 0:partIndex).mirrorUrl));
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}

	public int getItemPosition(String item,ArrayList<String> parent){
		int ret = -1;
		if (item!=null && !item.equals("") && parent!=null) {
			ret = parent.indexOf(item);
		}
		return ret;
	}
	
	private void initVideoView(){
		if (mListFilmPart == null) return;
//		mVideoView.setVideoURI(Uri.parse(path));
		mVideoView.setVideoPath(changeLinkToStream(mListFilmPart.
				get(partIndex>(mListFilmPart.size()) ? 0:partIndex).mirrorUrl));
//		mVideoView.setVideoQuality(getVideoQuality(AppSettingPrefference.getInstance().getMovieQuality()));
		mVideoView.setVideoChroma(getVideoChroma());
		mVideoView.setOnCompletionListener(this);
		mVideoView.setMediaController(new MediaController(mActivity));
		mVideoView.requestFocus();
		mVideoView.setOnInfoListener(this);
		mVideoView.setOnPreparedListener(this);
		mVideoView.setBufferSize(10*1024);
		mVideoView.setOnBufferingUpdateListener(this);
	}
	
	private String changeLinkToStream(String link){
		String ret = link;
		if (ret == null) {
			return "";
		}
		if (link.startsWith("http")) {
			ret = link.replaceFirst("http", "rtsp");
		}else if (link.startsWith("https")) {
			ret = link.replaceFirst("https", "rtsp");
		}
		return ret;
	}
	
	private int getVideoQuality(String quality){
//		String quality = AppSettingPrefference.getInstance().getMovieQuality();
		if(quality==null || quality.equals("")) return MediaPlayer.VIDEOQUALITY_MEDIUM;
		if (quality.equals(VIDEO_QUALITY_240P)) {
			return MediaPlayer.VIDEOQUALITY_LOW;
		}else if (quality.equals(VIDEO_QUALITY_360p)) {
			return MediaPlayer.VIDEOQUALITY_MEDIUM;
		}else if (quality.equals(VIDEO_QUALITY_720p)) {
			return MediaPlayer.VIDEOQUALITY_HIGH;
		}else{
			return MediaPlayer.VIDEOQUALITY_MEDIUM;
		}
	}
	
	private int getVideoChroma(){
		String chroma = AppSettingPrefference.getInstance().getMovieChroma();
		if(chroma==null || chroma.equals("")) return MediaPlayer.VIDEOCHROMA_RGB565;
		if (chroma.equals(VIDEO_CHROMA_RGB565)) {
			return MediaPlayer.VIDEOCHROMA_RGB565;
		}else if (chroma.equals(VIDEO_CHROMA_RGBA8888)) {
			return MediaPlayer.VIDEOCHROMA_RGBA;
		}else{
			return MediaPlayer.VIDEOCHROMA_RGB565;
		}
	}
	
	public void onPause(){
		if (mVideoView.isPlaying()) {
			currentPosition = mVideoView.getCurrentPosition();
			mVideoView.pause();
		}
	}
	
	public void onResume(){
		if (!mVideoView.isPlaying()) {
			if (currentPosition!=0) {
				mVideoView.seekTo(currentPosition);
			}
		}
	}
	
	@Override
	public void onPrepared(MediaPlayer mediaPlayer) {
		Log.d("onPrepared", "onPrepared");
		mediaPlayer.setPlaybackSpeed(1.0f);
		if (!mVideoView.isPlaying()) {
			mVideoView.start();
			mProgressBar.setVisibility(View.INVISIBLE);
		}
	}

	@Override
	public void onCompletion(MediaPlayer mediaPlayer) {
		Log.d("onCompletion", "onCompletion");
	}

	@Override
	public boolean onInfo(MediaPlayer arg0, int arg1, int arg2) {
		Log.d("onInfo", "onInfo");
		return false;
	}

	@Override
	public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
		Log.d("onBufferingUpdate - "+arg0+" - "+arg1, "onBufferingUpdate - "+arg0+" - "+arg1);
	}
	
}
